#include "file.h"

string g_moudlePath = "";

void InitModulePath()
{
	g_moudlePath = "";
	const int PATH_SIZE = 512;
	char path[PATH_SIZE] = { 0 };

#ifdef WIN32
	GetModuleFileNameA(NULL, (char*)&path, PATH_SIZE - 1);

	size_t pos = strlen(path);
	while(pos > 0)
	{
		if(path[pos--] == '\\')
		{
			break;
		}
	}
#else
	readlink("/proc/self/exe", path, PATH_SIZE - 1);

	int pos = strlen(path);
	while(pos > 0)
	{
		if(path[pos--] == '/')
			break;
	}

#endif

	path[pos + 2] = 0;
	g_moudlePath = path;
}

bool RemoveFile(string file)
{
	if(remove(file.c_str()) == 0)
		return true;
	else
		return false;
}

bool ReadFile(string file, char* &buf, size_t &length)
{
	buf = NULL;
	length = 0;

	FILE* fp = fopen(file.c_str(), "wb+");
	if(fp != NULL)
	{
		fseek(fp, 0, SEEK_END);
		length = ftell(fp);
		fseek(fp, 0, SEEK_SET);

		char* buf = SAFE_NEW char[length];
		fread(buf, length, 1, fp);

		fclose(fp);

		return true;
	}
	
	return false;
}

bool WriteFile(string file, char* buf, size_t length)
{
	FILE* fp = fopen(file.c_str(), "wb+");

	if(fp != NULL)
	{
		fseek(fp, 0, SEEK_END);
		fwrite(buf, length, 1, fp);
		fclose(fp);

		return true;
	}

	return false;
}

bool ReadStream(vector<string>& fileString, string file)
{
	std::ifstream fStream(file.c_str(), std::ios_base::in|std::ios::binary);
	if(fStream.good())
	{
		string line;
		while(getline(fStream, line))
			fileString.push_back(line);
		
		return true;
	}
	
	return false;
}

bool WriteStream(vector<string>& fileString, string file)
{
	std::ofstream os;
	os.open(file.c_str(), std::ios_base::out | std::ios_base::app);
	if(os.good())
	{
		size_t size = 0;
		while(size < fileString.size())
			os << fileString[size++] << std::endl;
	}
	else
		return false;

	os.close();
	return true;
}

////////////////////////////////////////////  XML  ////////////////////////////////////////////////

int32 getIntValue(TiXmlNode* node, const char* name)
{
	if(node == NULL || name == NULL)
		return 0;

	TiXmlElement* element = node->FirstChildElement(name);

	if(element == NULL || element->GetText() == NULL)
		return 0;

	string str = element->GetText();

	string::size_type find = str.find_first_not_of("-0123456789");

	if(find == string::npos)
		return atoi(element->GetText());
	else
		return 0;
}

const char* getStringValue(TiXmlNode* node, const char* name)
{
	if(node == NULL || name == NULL)
		return "";

	TiXmlElement* element = node->FirstChildElement(name);

	if(element == NULL || element->GetText() == NULL)
		return "";

	return element->GetText();
}

double getDoubleValue(TiXmlNode* node, const char* name)
{
	if(node == NULL || name == NULL)
		return 0.0;

	TiXmlElement* element = node->FirstChildElement(name);

	if(element == NULL || element->GetText() == NULL)
		return 0.0;

	string str = element->GetText();

	string::size_type find = str.find_first_not_of("-.0123456789");

	if(find == string::npos)
		return atof(element->GetText());
	else
		return 0.;
}

int32 getIntAttribute(TiXmlElement* element, const char* name)
{
	if(element == NULL || element == NULL || element->Attribute(name) == NULL)
		return 0;

	int32 num;
	
	element->Attribute(name, &num);

	return num;
}

double getDoubleAttribute(TiXmlElement* element, const char* name)
{
	if(element == NULL || element == NULL || element->Attribute(name) == NULL)
		return 0;

	double num;

	element->Attribute(name, &num);

	return num;
}

const char* getStringAttribute(TiXmlElement* element, const char* name)
{
	if(element == NULL || element == NULL || element->Attribute(name) == NULL)
		return "";

	return element->Attribute(name);
}